<?php

namespace app\index\controller;

use addons\shopro\library\commission\Agent;
use addons\shopro\library\commission\Log;
use addons\shopro\model\commission\Agent as AgentModel;
use addons\shopro\model\commission\Config as ConfigModel;
use addons\shopro\model\commission\Reward as RewardModel;
use addons\shopro\model\User as UserModel;
use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
    public function test(){
        $a = $this->get_boss(136);
        dump($a);
    }

    public function get_boss($user_id){
        $boss = AgentModel::where('user_id',$user_id)->field('user_id,parent_agent_id,level,bf_status')->find();
//        echo AgentModel::getLastSql().'<br/>';
//        file_put_contents('./boss.txt', var_export(date('Y-m-d H:i:s').'----userid:'.$user_id,true)."\r\n", FILE_APPEND);
//        file_put_contents('./boss.txt', var_export($boss['level'],true)."\r\n", FILE_APPEND);
//        file_put_contents('./boss11.txt', var_export($boss,true)."\r\n", FILE_APPEND);
        if(!$boss){
            return [];
        }elseif($boss->level == 2){
            return $boss;
        }else{
            return $this->get_boss($boss['parent_agent_id']);
        }
    }

    public function check_bf($user_id)
    {
        file_put_contents('./fenyong2.txt', var_export(date('Y-m-d H:i:s').'---'.$user_id,true)."\r\n", FILE_APPEND);
        if(!$user_id){
            return true;
        }
        $is_bf = 1;
        //查询当前代理商信息
        $agent = AgentModel::where('user_id',$user_id)->find();
//        file_put_contents('./fenyong2.txt', var_export($agent,true)."\r\n", FILE_APPEND);
        //如果当前用户不是老板，且帮扶状态
        if($agent['level'] != 2 || $agent['bf_status'] == 1){
            $this->check_bf($agent['parent_agent_id']);
        }else{
            //查询此用户下级有几个老板
            $boss_num = AgentModel::where('parent_agent_id',$agent['user_id'])->where('level',2)->count();
//           file_put_contents('./fenyong2.txt', var_export(date('Y-m-d H:i:s').'---'.$user_id.'boss_num1：'.$boss_num,true)."\r\n", FILE_APPEND);
            $config = ConfigModel::column('value', 'name');
            if($boss_num < $config['bf_boss_num']){
                $is_bf = 0;
            }
            //下级代理数量
            $zhitui = AgentModel::where('parent_agent_id',$agent['user_id'])->column('user_id');
            $daili_res_num = 0;
            foreach ($zhitui as $v){
                $daili_num = $this->getTeamAgents($v);
                $daili_res_num += $daili_num;
            }

            if($daili_res_num < $config['bf_daili_num']){
                $is_bf = 0;
            }
            file_put_contents('./fenyong2.txt', var_export(date('Y-m-d H:i:s').'---'.$user_id.'boss_num：'.$boss_num.'---daili:'.$daili_num,true)."\r\n", FILE_APPEND);

            $this->check_bf($agent['parent_agent_id']);
        }
    }

    public function getTeamAgents($team_id)
    {
        // 查询当前团队下的代理数量
        $agents = AgentModel::where('parent_agent_id', $team_id)->where('level',1)->count();
        // 查询下级团队的代理数量
        $subTeams = AgentModel::where('parent_agent_id', $team_id)->field('user_id,level,parent_agent_id')->select();
        foreach ($subTeams as $subTeam) {
            $subAgents = $this->getTeamAgents($subTeam['user_id']); // 递归查询下级团队的代理数量
            $agents += $subAgents; // 累加下级团队的代理数量
        }
        return $agents;
    }

}
